package com.ylz.sjzx.sio.sdk.client.core;

import com.corundumstudio.socketio.SocketIOClient;
import com.ylz.sjzx.sio.common.core.context.ApplicationUtils;
import com.ylz.sjzx.sio.sdk.client.core.socketio.ImEncoderHandler;
import com.ylz.sjzx.sio.sdk.client.session.IMSession;
import com.ylz.sjzx.sio.sdk.client.session.UserSession;
import io.netty.channel.Channel;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author wiqi
 * @version 1.0
 * @date 2020/12/8
 */
public class IMNioBase {

    /**
     * 读空闲时间(秒)
     */
    public static final int READ_IDLE_TIME = 3600;
    /**
     * 写接空闲时间(秒)
     */
    public static final int WRITE_IDLE_TIME = 600;
    /**
     * 心跳响应 超时为10秒
     */
    public static final int PONG_TIME_OUT = 10;
    public static ImEncoderHandler encoderHandler;
    /**
     * 是否在运行
     */
    public static volatile boolean isGlobalRun = true;
    public static Object objLock = new Object();
    private static ConcurrentMap<String, SocketIOClient> socketIOClientMap = new ConcurrentHashMap<>();
    private static ConcurrentMap<String, IMSession> globalSession = new ConcurrentHashMap<>();
    /**
     * 初始绑定channel
     */
    private static ConcurrentHashMap<String, Channel> globalChannel = new ConcurrentHashMap<>();
    /**
     * 用户session 凌晨一点需要清空
     * 需要注意如果用户量过大的话  需要增加清空时间
     */
    private static ConcurrentHashMap<String, UserSession> globalUserSession = new ConcurrentHashMap<>();
    /**
     * 老师是否接收消息
     */
    private static Boolean needTeacherGetMsg = null;
    /**
     * 配置上下文
     */
    private static ThreadLocal<IMSession> tSession = new ThreadLocal<>();
    /**
     * tempauth用户访问
     */
    private static Boolean tempauth;

    /**
     * 防止有用户未清除
     */
    public static void cleanGlobal() {
        socketIOClientMap = new ConcurrentHashMap<>(16);
        globalSession = new ConcurrentHashMap<>(16);
        globalChannel = new ConcurrentHashMap<>(16);
        globalUserSession = new ConcurrentHashMap<>(16);
    }

    public static boolean isNeedTeacherGetMsg() {
        if (needTeacherGetMsg == null) {
            needTeacherGetMsg = Boolean.valueOf(ApplicationUtils.getProperty("base.needTeacherGetMsg"));
        }
        return needTeacherGetMsg;
    }

    public static void clean() {
        tSession.remove();
    }

    public static Boolean getTempauth() {
        if (tempauth == null) {
            synchronized (objLock) {
                if (tempauth == null) {
                    tempauth = Boolean.valueOf(ApplicationUtils.getProperty("im.tempauth", "true"));
                }
            }
        }
        return tempauth;
    }

    public static ConcurrentMap<String, IMSession> getGlobalSession() {
        return globalSession;
    }


    public static IMSession getSession() {
        return tSession.get();
    }

    public static void setSession(IMSession session) {
        tSession.set(session);
    }


    public static ConcurrentHashMap<String, Channel> getGlobalChannel() {
        return globalChannel;
    }

    public static ConcurrentMap<String, SocketIOClient> getSocketIOClientMap() {
        return socketIOClientMap;
    }

    public static ConcurrentHashMap<String, UserSession> getGlobalUserSession() {
        return globalUserSession;
    }

}
